//
//  CZMyTaskViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/9.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZMyTaskViewController.h"
#import "CZTaskListViewController.h"
#import "CZMyTaskSubViewController.h"
@interface CZMyTaskViewController ()
@property (weak, nonatomic) IBOutlet UIButton *taskingBtn;
@property (weak, nonatomic) IBOutlet UIButton *completeTaskBtn;
@property (weak, nonatomic) IBOutlet UIButton *rewardedBtn;
@property (weak, nonatomic) IBOutlet UIView *yellowView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (nonatomic, strong) CZMyTaskSubViewController *taskingVC;
@property (nonatomic, strong) CZMyTaskSubViewController *completeVC;
@property (nonatomic, strong) CZMyTaskSubViewController *rewardedVC;

@end

@implementation CZMyTaskViewController {
    CGFloat _currentOffect;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CZMyTaskSubViewController *taskingVC = [[CZMyTaskSubViewController alloc] init];
    _taskingVC = taskingVC;
    [self addChildViewController:taskingVC];
    
    CZMyTaskSubViewController *completeVC = [[CZMyTaskSubViewController alloc] init];
    _completeVC = completeVC;
    [self addChildViewController:completeVC];
    
    CZMyTaskSubViewController *rewardedVC = [[CZMyTaskSubViewController alloc] init];
    _rewardedVC = rewardedVC;
    [self addChildViewController:rewardedVC];
    
    taskingVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:taskingVC.view];
    taskingVC.status = @0;
    [taskingVC postMyTaskDataWithStatus:@0];
    
    _currentOffect = Main_Screen_Width / 6;
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)rewardedBtnAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    sender.selected = YES;
    sender.highlighted = NO;
    _taskingBtn.selected = NO;
    _completeTaskBtn.selected = NO;
    _taskingBtn.highlighted = YES;
    _completeTaskBtn.highlighted = YES;
    
    _rewardedVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:_rewardedVC.view];
    _rewardedVC.status = @2;

    [_rewardedVC postMyTaskDataWithStatus:@2];
    [self setAnimationWithKey:@"positionRight"];
    
    _currentOffect = Main_Screen_Width / 6 * 5;
}
- (IBAction)completeTaskAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    sender.selected = YES;
    sender.highlighted = NO;
    _rewardedBtn.selected = NO;
    _taskingBtn.selected = NO;
    _rewardedBtn.highlighted = YES;
    _taskingBtn.highlighted = YES;
    _completeVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:_completeVC.view];
    _completeVC.status = @1;

    [_completeVC postMyTaskDataWithStatus:@1];
    [self setAnimationWithKey:@"positionCenter"];
    _currentOffect = Main_Screen_Width / 2;
}

- (IBAction)taskingAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    sender.selected = YES;
    sender.highlighted = NO;
    
    _rewardedBtn.selected = NO;
    _completeTaskBtn.selected = NO;
    _rewardedBtn.highlighted = YES;
    _completeTaskBtn.highlighted = YES;
    
    _taskingVC.view.frame = self.contentView.bounds;
    [self.contentView addSubview:_taskingVC.view];
    _taskingVC.status = @0;
    [_taskingVC postMyTaskDataWithStatus:@0];
    [self setAnimationWithKey:@"positionLeft"];
    _currentOffect = Main_Screen_Width / 6;

}
- (void)setAnimationWithKey:(NSString *)key {
    [_yellowView.layer removeAllAnimations];
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    basicAnimation.fromValue = @(_currentOffect);
    if ([key isEqualToString:@"positionLeft"]) {
        basicAnimation.toValue = @(Main_Screen_Width / 6);
    } else if ([key isEqualToString:@"positionCenter"]) {
        basicAnimation.toValue = @(Main_Screen_Width / 2);
    } else {
        basicAnimation.toValue = @(Main_Screen_Width / 6 * 5);
    }
    basicAnimation.duration = 0.2;
    basicAnimation.fillMode = kCAFillModeForwards;
    basicAnimation.removedOnCompletion = NO;
    [_yellowView.layer addAnimation:basicAnimation forKey:key];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
